package com.sskj.bibi.ui.fragment;


import android.support.constraint.ConstraintLayout;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.widget.RxRadioGroup;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.CoinUpdateBean;
import com.sskj.bibi.component.DaggerUserDataComponent;
import com.sskj.bibi.presenter.BibiBuyAndSellFragmentPresenter;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.depth.bean.DepthData;
import com.sskj.depth.bean.IDepthData;
import com.sskj.depth.view.DepthMapView;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.BibiCoinType;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.bean.enums.ColorEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.NumberUtil;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * 币币交易主页购买区域
 */
@Route(path = RConfig.BIBI_FRAGMENT_BUY_AND_SELL)
public class BibiBuyAndSellFragment extends BaseFragment<BibiBuyAndSellFragmentPresenter> {
    @BindView(R2.id.rbBuy)
    RadioButton rbBuy;
    @BindView(R2.id.rbSell)
    RadioButton rbSell;
    @BindView(R2.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R2.id.spinner)
    TextView spinner;
    @BindView(R2.id.llPriceMode)
    LinearLayout llPriceMode;
    @BindView(R2.id.etPrice)
    EditText etPrice;

    @BindView(R2.id.llAddAndDe)
    LinearLayout llAddAndDe;
    @BindView(R2.id.rlEditPrice)
    RelativeLayout rlEditPrice;
    @BindView(R2.id.llHidePrice)
    LinearLayout llHidePrice;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.tvDecrease)
    TextView tvDecrease;
    @BindView(R2.id.tvIncrease)
    TextView tvIncrease;
    @BindView(R2.id.tvCoinType)
    TextView tvCoinType;
    @BindView(R2.id.tvMoney)
    TextView tvMoney;
    @BindView(R2.id.tvTradeMoney)
    TextView tvTradeMoney;
    @BindView(R2.id.btBuy)
    Button btBuy;
    @BindView(R2.id.rgLevel)
    RadioGroup rgLevel;
    @BindView(R2.id.tvNumTitle)
    TextView tvNumTitle;
    @BindView(R2.id.tvMoneyTitle)
    TextView tvMoneyTitle;
    @BindView(R2.id.tvMoneyType)
    TextView tvMoneyType;
    @BindView(R2.id.tvHideMoneyType)
    TextView tvHideMoneyType;
    @BindView(R2.id.tvAllMoneyTitle)
    TextView tvAllMoneyTitle;
    @BindView(R2.id.depthMapView)
    DepthMapView depthMapView;
    @BindView(R2.id.indicatorSeekBar)
    IndicatorSeekBar indicatorSeekBar;
    @BindView(R2.id.etPriceCf)
    EditText etPriceCf;
    @BindView(R2.id.add_cf)
    TextView addCf;
    @BindView(R2.id.del_cf)
    TextView delCf;
    @BindView(R2.id.zyzs)
    ConstraintLayout zyzs;

    @Autowired(required = true)
    String code = "BTC/USDT";//币种类型code
    String leftCode = "BTC";//币种类型
    String rightCode = "USDT";//币种类型
    private BottomSheetDialog priceModeSheet;
    private boolean isBuy = true;
    private boolean isMarketPrice = false;
    private String marketPrice = "5000";//市价
    private String currentPrice = "5000";//限价
    private String rightMoney = "0";//可用USDT
    private String leftMoney = "0";//可用币
    private String rate = "0.0001";//加减的点
    private String maxTradeCoin = "0";//最大可交易币种数量
    private String fee = "0";//手续费
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private int numKeepDot;
    private boolean isSetText;

    private boolean isZyZs = false;
    private String cfPrice = "0";
    private double buyRate = 100;
    private double sellRate = 100;
    private Disposable priceDispo;
    private Disposable numDispo;

    @Override
    protected int getLayoutId() {
        return R.layout.bibi_fragment_buy_and_sell;
    }

    @Override
    public BibiBuyAndSellFragmentPresenter getPresenter() {
        return new BibiBuyAndSellFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);

        btBuy.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.bibi_shape_green : R.drawable.bibi_shape_red));
        ClickUtil.click(btBuy, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_LOGIN).navigation();
        });
        depthMapView.setDrawText(false);

        code = CoinUtil.showName(code);
        leftCode = CoinUtil.getLCode(code);
        rightCode = CoinUtil.getRCode(code);
        rate = new BigDecimal("0.1").pow(CoinUtil.getNumKeepNum(code)).toPlainString();


        initClick();
        updateUI();
    }
    @Override
    public void initData() {
        mPresenter.getDepthData(code);
        mPresenter.initSocket(code);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            mPresenter.getCoinInfo(code, userData);
        });
        LiveDataBus.get().with(RxBusCode.BIBI_FRESH).observe(this, o -> refresh());
        LiveDataBus.get().with(RxBusCode.BIBI_CHANGE_COIN, BibiCoinType.class).observe(this, this::changeCoin);
        LiveDataBus.get().with(RxBusCode.CHANGE_PRICE, String.class)
                .observe(this, s -> {
                    if (!isMarketPrice) {
                        etPrice.setText(CoinUtil.keepCoinPrice(code, s));
                        etPrice.setSelection(etPrice.getText().toString().length());
                    }
                });
    }

    /**
     * 更新界面根据
     * 买入卖出，市价限价，币种，可用金额 变化 初始化界面
     */
    private void updateUI() {
        calculateMaxTrade();
        numKeepDot = CoinUtil.getNumKeepNum(code);
        tvHideMoneyType.setText(rightCode);
        radioGroup.check(isBuy ? R.id.rbBuy : R.id.rbSell);
        updateIndicator();
        if (isZyZs) {
            spinner.setText("止盈止损");
            zyzs.setVisibility(View.VISIBLE);
            rlEditPrice.setVisibility(View.VISIBLE);
            llHidePrice.setVisibility(View.GONE);
            etPriceCf.setText("");
        } else {
            zyzs.setVisibility(View.GONE);
            spinner.setText(isMarketPrice ? "市价交易" : "限价交易");
            rlEditPrice.setVisibility(isMarketPrice ? View.GONE : View.VISIBLE);
            llHidePrice.setVisibility(isMarketPrice ? View.VISIBLE : View.GONE);
        }
        etNum.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(CoinUtil.getNumKeepNum(code))});
        etPrice.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(CoinUtil.getPriceKeepNum(code))});
        etPriceCf.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(CoinUtil.getPriceKeepNum(code))});
        etPrice.setText(CoinUtil.keepCoinPrice(code, marketPrice));
        etNum.setText("");
        indicatorSeekBar.setProgress(0);
        if (isMarketPrice && isBuy) {
            tvNumTitle.setText("交易额");
        } else {
            tvNumTitle.setText("数量");
        }
        rgLevel.clearCheck();
        tvMoney.setText(String.format("%s%s", isBuy ? CoinUtil.keepCoinMoney(code, rightMoney) : CoinUtil.keepCoinNum(code, leftMoney), isBuy ? rightCode : leftCode));
        tvCoinType.setText((isBuy && isMarketPrice) ? rightCode : leftCode);
        btBuy.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.bibi_shape_32b_corner10 : R.drawable.bibi_shape_ff5_corner10));
        if (userData != null) {
            btBuy.setText(isBuy ? "买入" : "卖出");
            ClickUtil.click(btBuy, () -> {
                if (checkParam()) {
                    mPresenter.submit(code, isMarketPrice ? marketPrice : currentPrice,
                            etNum.getText().toString(), isBuy ? "1" : "2", isZyZs ? "3" : isMarketPrice ? "1" : "2", etPriceCf.getText().toString());
                }
            });
        } else {
            btBuy.setText("请登录");
            ClickUtil.click(btBuy, () -> {
                ARouter.getInstance().build(RConfig.LOGIN_LOGIN).navigation();
            });
        }
        changeShowData();
    }

    private void updateIndicator() {
        indicatorSeekBar.setProgress(0);
        indicatorSeekBar.setThumbDrawable(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.bb_icon_2 : R.drawable.bb_icon_4));
        indicatorSeekBar.setTickMarksDrawable(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.bibi_selector_marks_drawable_green : R.drawable.bibi_selector_marks_drawable_red));
        indicatorSeekBar.setProgressTrackColor(ColorEnum.getColor(isBuy));
    }

    /**
     * 计算最大可输入数量
     */
    private void calculateMaxTrade() {
        if (TextUtils.isEmpty(currentPrice) || Double.valueOf(currentPrice) == 0) {
            maxTradeCoin = "0";
        } else {
            if (isBuy) {
                if (isMarketPrice) {
                    maxTradeCoin = NumberUtil.keepDown(new BigDecimal(rightMoney).divide(new BigDecimal("1").add(new BigDecimal(fee)), 8, BigDecimal.ROUND_HALF_UP).toString(), CoinUtil.getPriceKeepNum(code), true);
                } else {
                    maxTradeCoin = NumberUtil.keepDown(new BigDecimal(rightMoney).divide(new BigDecimal("1").add(new BigDecimal(fee)), 8, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(currentPrice), 8, RoundingMode.HALF_UP).toString(), CoinUtil.getNumKeepNum(code), true);
                }
            } else {
                maxTradeCoin = NumberUtil.keepDown(new BigDecimal(leftMoney).toString(), CoinUtil.getNumKeepNum(code), true);
            }
        }

        if (isBuy && isMarketPrice) {
//          indicatorSeekBar.customTickTexts(new String[]{"0", "", "", "", maxTradeCoin + " " + rightCode});
            indicatorSeekBar.customTickTexts(new String[]{"0", "", "", "", CoinUtil.keepCoinMoney(code, rightMoney) + " " + rightCode});
            String s = CoinUtil.keepCoinMoney(code, new BigDecimal(maxTradeCoin).multiply(new BigDecimal(Math.pow(10, numKeepDot))).toPlainString());
            indicatorSeekBar.setMax(Float.valueOf(s));
            indicatorSeekBar.setProgress(0);

        } else {
            indicatorSeekBar.customTickTexts(new String[]{"0", "", "", "", maxTradeCoin + " " + leftCode});
            String s = CoinUtil.keepCoinNum(code, new BigDecimal(maxTradeCoin).multiply(new BigDecimal(Math.pow(10, numKeepDot))).toPlainString());
            indicatorSeekBar.setMax(Float.valueOf(s));
            indicatorSeekBar.setProgress(0);
        }
    }

    /**
     * 检测购买条件
     */
    private boolean checkParam() {
        if (!AppCircleCheckUtil.checkLogin(getActivity(),userData)){
            return false;
        }
        if (isMarketPrice) {
            if (TextUtils.isEmpty(etNum.getText()) || Double.valueOf(etNum.getText().toString()) == 0) {
                ToastUtil.showShort(String.format("请输入数量", isBuy ? "购买" : "出售"));
                return false;
            }
        } else {
            if (isZyZs) {
                if (TextUtils.isEmpty(etPriceCf.getText().toString())) {
                    ToastUtil.showShort("请输入正确触发价格");
                    return false;
                }
                if (isBuy) {
                    if (Double.valueOf(etPriceCf.getText().toString()) > (buyRate / 100) * Double.valueOf(etPrice.getText().toString())) {
                        ToastUtil.showShort(String.format("委托价不能高于触发价的%s", buyRate + "%"));
                        return false;
                    }
                } else {
                    if (Double.valueOf(etPriceCf.getText().toString()) < (sellRate / 100) * Double.valueOf(etPrice.getText().toString())) {
                        ToastUtil.showShort(String.format("委托价不能低于触发价的%s", sellRate + "%"));
                        return false;
                    }
                }
            }
            if (TextUtils.isEmpty(etPrice.getText()) || Double.valueOf(etPrice.getText().toString()) == 0) {
                ToastUtil.showShort(String.format("请输入%s价格", isBuy ? "购买" : "出售"));
                return false;
            }
            if (TextUtils.isEmpty(etNum.getText()) || Double.valueOf(etNum.getText().toString()) == 0) {
                ToastUtil.showShort(String.format("请输入%s数量", isBuy ? "购买" : "出售"));
                return false;
            }
        }
        return true;
    }

    /**
     * 根据
     * 价格，数量，滑竿更新交易额
     */
    private void changeShowData() {
        if (TextUtils.isEmpty(etPrice.getText()) || TextUtils.isEmpty(etNum.getText())) {
            if (isBuy) {
                tvAllMoneyTitle.setText(String.format("交易额 %s：", rightCode));
            } else {
                if (isMarketPrice) {
                    tvAllMoneyTitle.setText(String.format("交易量 %s： ", leftCode));
                } else {
                    tvAllMoneyTitle.setText(String.format("交易额 %s：", rightCode));
                }
            }
            tvTradeMoney.setText("0");
        } else {
            if (!isMarketPrice) {
                String usdtFull;
                if (isBuy) {
                    usdtFull = new BigDecimal("1").add(new BigDecimal(fee)).multiply(new BigDecimal(etNum.getText().toString())
                            .multiply(new BigDecimal(etPrice.getText().toString()))).toString();
                } else {
                    usdtFull = new BigDecimal("1").multiply(new BigDecimal(etNum.getText().toString()).multiply(new BigDecimal(etPrice.getText().toString()))).toString();
                }
                String usdt;
                if (isBuy) {
                    //限价买入
                    usdt = CoinUtil.keepCoinMoney(code, usdtFull);
                    tvTradeMoney.setText(usdt);
                    tvAllMoneyTitle.setText(String.format("交易额 %s： ", rightCode));

                } else {//限价卖出
                    usdt = CoinUtil.keepCoinMoney(code, usdtFull);
                    tvTradeMoney.setText(usdt);
                    tvAllMoneyTitle.setText(String.format("交易额 %s： ", rightCode));
                }
            } else {
                String usdtFull;
                if (isBuy) {
                    usdtFull = new BigDecimal("1").add(new BigDecimal(fee)).multiply(new BigDecimal(etNum.getText().toString())).toString();

                } else {
                    usdtFull = new BigDecimal("1").multiply(new BigDecimal(etNum.getText().toString())).toString();
                }
                String usdt;
                if (isBuy) {
                    //市价买入
                    usdt = CoinUtil.keepCoinMoney(code, usdtFull);
                    tvAllMoneyTitle.setText(String.format("交易额 %s： ", rightCode));
                    tvTradeMoney.setText(usdt);
                } else {//市价卖出
                    usdt = CoinUtil.keepCoinNum(code, usdtFull);
                    tvAllMoneyTitle.setText(String.format("交易量 %s： ", leftCode));
                    tvTradeMoney.setText(usdt);
                }
            }
        }
    }

    private void initClick() {

        RxRadioGroup.checkedChanges(radioGroup)
                .subscribe(clickId -> {
                    if (!isBuy && clickId == rbBuy.getId()) {//购买
                        isBuy = true;
                        updateUI();
                    } else if (isBuy && clickId == rbSell.getId()) {//出售
                        isBuy = false;
                        updateUI();
                    }
                });

        ClickUtil.click(llPriceMode, () -> {
            if (priceModeSheet == null) {
                priceModeSheet = BottomSheetUtil.getBottomSheet(getActivity(), null, (recyclerView, position, v) -> {
                    priceModeSheet.dismiss();
                    if (position == 0) {
                        isMarketPrice = false;
                        isZyZs = false;
                        updateUI();

                    } else if (position == 1) {
                        isMarketPrice = true;
                        isZyZs = false;
                        updateUI();
                    } else if (position == 2) {
                        isMarketPrice = false;
                        isZyZs = true;
                        updateUI();
                    }
                }, "限价交易", "市价交易", "止盈止损");
            }
            priceModeSheet.show();
        });
        ClickUtil.click(100, tvDecrease, () -> {
            if (TextUtils.isEmpty(etPrice.getText())) {
                currentPrice = "0";
            }
            String decrease = "-" + rate;
            BigDecimal priceDecimal = new BigDecimal(currentPrice).add(new BigDecimal(decrease));
            if (priceDecimal.doubleValue() <= 0) {
                etPrice.setText("0");
                etPrice.setSelection(etPrice.getText().length());
            } else {
                etPrice.setText(CoinUtil.keepCoinPrice(code, priceDecimal.toString()));
                etPrice.setSelection(etPrice.getText().toString().length());
            }
        });
        ClickUtil.click(100, tvIncrease, () -> {
            if (TextUtils.isEmpty(etPrice.getText())) {
                currentPrice = "0";
            }
            String increase = rate;
            BigDecimal priceDecimal = new BigDecimal(currentPrice).add(new BigDecimal(increase));
            etPrice.setText(CoinUtil.keepCoinPrice(code, priceDecimal.toString()));
            etPrice.setSelection(etPrice.getText().toString().length());


        });

        ClickUtil.click(100, delCf, () -> {
            if (TextUtils.isEmpty(etPriceCf.getText())) {
                cfPrice = "0";
            } else {
                cfPrice = etPriceCf.getText().toString();
            }
            String decrease = "-" + rate;
            BigDecimal priceDecimal = new BigDecimal(cfPrice).add(new BigDecimal(decrease));
            if (priceDecimal.doubleValue() <= 0) {
                etPriceCf.setText("0");
                etPriceCf.setSelection(etPriceCf.getText().length());
            } else {
                etPriceCf.setText(CoinUtil.keepCoinPrice(code, priceDecimal.toString()));
                etPriceCf.setSelection(etPriceCf.getText().toString().length());
            }
        });
        ClickUtil.click(100, addCf, () -> {
            if (TextUtils.isEmpty(etPriceCf.getText())) {
                cfPrice = "0";
            } else {
                cfPrice = etPriceCf.getText().toString();
            }
            String increase = rate;
            BigDecimal priceDecimal = new BigDecimal(cfPrice).add(new BigDecimal(increase));
            etPriceCf.setText(CoinUtil.keepCoinPrice(code, priceDecimal.toString()));
            etPriceCf.setSelection(etPriceCf.getText().toString().length());


        });


        indicatorSeekBar.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {
                if (seekParams.fromUser) {
                    if (userData == null) {
                        return;
                    }
                    String s = new BigDecimal(seekParams.progressFloat).divide(new BigDecimal(Math.pow(10, numKeepDot)),
                            8, BigDecimal.ROUND_HALF_UP).toPlainString();

                    isSetText = true;
//                    String text = (isBuy && isMarketPrice) ? CoinUtil.keepCoinPrice(code, s) : CoinUtil.keepCoinNumMax2(code, s);
                    String text = CoinUtil.keepCoinNum(code, s);
                    etNum.setText(text);
                    etNum.setSelection(etNum.getText().toString().length());//将光标移至文字末尾
                    isSetText = false;
                }
            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

            }
        });
        initInputChange();
    }

    private void initInputChange() {
        DisposUtil.close(priceDispo);
        DisposUtil.close(numDispo);
        priceDispo = RxTextView.afterTextChangeEvents(etPrice)
                .subscribe(value -> {
                    String text = value.editable().toString();
                    if (TextUtils.isEmpty(text)) {
                        currentPrice = "0";
                    } else {
                        currentPrice = text;
                    }
                    calculateMaxTrade();
                    changeShowData();
                    etNum.setText(etNum.getText());
                }, e -> System.out.println(e));
        numDispo = RxTextView.afterTextChangeEvents(etNum)
                .subscribe(value -> {
                    if (userData == null) {
                        return;
                    }
                    String text = value.editable().toString();
                    if (!TextUtils.isEmpty(text) && text.equals(".")) {
                        etNum.setText("0");
                        text = "0";
                    }
                    if (TextUtils.isEmpty(text) || Double.valueOf(text) == 0 ||
                            TextUtils.isEmpty(etPrice.getText()) || Double.valueOf(etPrice.getText().toString()) == 0) {
                        indicatorSeekBar.setProgress(0);
                    } else {
                        if (Double.valueOf(text) > Double.valueOf(maxTradeCoin)) {
                            etNum.setText(maxTradeCoin);
//                            ToastUtil.showShort("资金不足");
                        } else {
                            if (!isSetText) {
                                String s = new BigDecimal(text).multiply(new BigDecimal(Math.pow(10, numKeepDot))).toPlainString();
                                indicatorSeekBar.setProgress(Float.valueOf(s));
                            }
//                            int progress = new BigDecimal(text).divide(new BigDecimal(maxTradeCoin), 8, RoundingMode.HALF_UP).multiply(new BigDecimal("1000")).intValue();
//                            progress = progress > 1000 ? 1000 : progress;
                        }
                    }
                    changeShowData();
                });
    }



    /**
     * 更换币种,更改市价限价
     */
    public void changeCoin(BibiCoinType coinType) {

        code = coinType.getCode();
        rate = new BigDecimal("0.1").pow(CoinUtil.getNumKeepNum(code)).toPlainString();
        rightCode = CoinUtil.getRCode(code);
        leftCode = CoinUtil.getLCode(code);
        isMarketPrice = false;
        isBuy = true;
        refresh();
    }


    /**
     * 刷新界面
     */
    public void refresh() {
        mPresenter.getCoinInfo(code, userData);
        mPresenter.getDepthData(code);
        mPresenter.initSocket(code);
    }


    public void updateCoinData(CoinUpdateBean coinUpdateBean) {
        currentPrice = marketPrice = coinUpdateBean.getMarketPrice();
        rightMoney = coinUpdateBean.getRightMoney();
        leftMoney = coinUpdateBean.getLeftMoney();
        sellRate = TextUtils.isEmpty(coinUpdateBean.getSellRate()) ? 0d : Double.valueOf(coinUpdateBean.getSellRate());
        buyRate = TextUtils.isEmpty(coinUpdateBean.getBuyRate()) ? 0d : Double.valueOf(coinUpdateBean.getBuyRate());
        fee = new BigDecimal(coinUpdateBean.getFee()).multiply(new BigDecimal("1")).toPlainString();
        updateUI();

    }

    /**
     * 更新深度图
     *
     * @param data
     */
    public void updateDeep(WSFiveBean data) {
        if (data == null) {
            data = new WSFiveBean();
            data.setCode(code);
            data.setBids(new ArrayList<>());
            data.setAsks(new ArrayList<>());
        }
        Collections.reverse(data.getAsks());
        Collections.reverse(data.getBids());
        Flowable<List<IDepthData>> buyFlow = Flowable.fromIterable(data.getBids())
                .map(fiveBean -> (IDepthData) new DepthData(fiveBean.getDoubleTotalSize(), fiveBean.getPrice()))
                .toList()
                .toFlowable();
        Flowable<List<IDepthData>> sellFlow = Flowable.fromIterable(data.getAsks())
                .map(fiveBean -> (IDepthData) new DepthData(fiveBean.getDoubleTotalSize(), fiveBean.getPrice()))
                .toList()
                .toFlowable();

        Flowable.zip(buyFlow, sellFlow, (buyData, sellData) -> {
            if (depthMapView != null) {
                depthMapView.mPriceLimit = CoinUtil.getPriceKeepNum(code);
                depthMapView.mVolumeLimit = CoinUtil.getNumKeepNum(code);
                depthMapView.setData(buyData, sellData);
            }
            return "1";
        }).subscribe();
    }

    public void buySuccess() {
        LiveDataBus.get().with(RxBusCode.BIBI_FRESH).postValue(1);
//        ARouter.getInstance().build(RConfig.BIBI_RECORD).navigation();
    }


}
